# -*- coding: utf-8 -*-
"""
Created on 2022/2/24

@author: Song
"""

from DBTestAnalysisLib import *
import plotnine as G  # ;#ggplot,aes,geom_bar,facet_grid,theme,element_text,ggtitle,stat_summary,geom_text,after_stat
import pandas as pd
import numpy as np

pd.set_option('mode.chained_assignment', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', 500)
pd.set_option('display.width', 1000)

def spaceAnalysis():  # 总空间、空间明细
    dataSize = '.all'
    tmp = []
    for dataset in ['energy', 'traffic', 'syn']:
        systems = {}
        for db in ['TGK', 'PG', 'MA', 'N1', 'N2', 'TGC']:
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        df['dataset'] = dataset
        tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)

    allData['value'] = allData['value'] / (1024 * 1024)  # * 1024
    # allData.iloc[1,list(allData.columns).index('value')] = '老李'
    allData.loc[(allData["sys"]=="TGK") & (allData['category']=='tIndex'), "value"] = 0
    print(allData)
    # allData.to_csv('space-result-detail.csv')
    # allData.drop(columns=['', 'C'])
    # https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#aggregation
    grped = allData.groupby(["dataset", 'sys']).agg(
        # space=('value', lambda lst: '/'.join(['{:.1f}'.format(x) for x in lst]))
        space=('value', np.sum)
    )
    # https://stackoverflow.com/questions/42706638/transform-pandas-groupby-aggregate-result-to-dataframe
    toPrint = pd.DataFrame(grped.reset_index())
    # toPrint1 = toPrint.pivot(index='sys', columns='dataset', values='space')
    print(toPrint)
    # toPrint1.to_csv('space-result.csv')

    # return
    g = G.ggplot(allData, G.aes('sys', 'value', fill='category'))
    g += G.geom_col()
    # + G.scale_x_datetime(breaks=date_breaks('1 month'))
    # g = g + G.ggtitle('Space cost of Comparable systems (Bytes)')
    # g+= G.scale_y_continuous(labels=lambda l: ["%dM" % (v / 1000000) for v in l])
    g += G.ylab('Database Size (MB)')
    g += G.xlab('Systems')

    dodge_text = G.position_dodge(width=.9)
    # g = G.ggplot(spaces, G.aes(x='type', y='exeTime', fill='sys'))
    # g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    # g = g + G.geom_boxplot(width=.3, outlier_size=1, outlier_shape='.', position=dodge_text)
    # g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.sum),
    #                     position=dodge_text, format_string='{:.1f}') #, va='bottom'
    g += G.geom_text(
        G.aes(label=G.after_stat('y')),
        stat=G.stat_summary(fun_y=np.sum),
        position=G.position_stack(vjust=0.5),
        # https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#aggregation
        # adjust_text={  # https://stackoverflow.com/questions/57701052/how-do-i-use-adjust-text-with-plotnine
        #     # 'add_objects': 'bars',
        #     # 'autoalign': True,
        #     # 'only_move': {'points': 'value', 'text': 'value', 'objects': 'value'},
        #     # 'ha': 'center',
        #     # 'va': 'bottom'
        #     #     'expand_points': (2, 2),
        #     #     'arrowprops': {
        #     #         'arrowstyle': '->',
        #     #         'color': 'red'
        #     #     }
        # },
        format_string='{:.1f}',
        size=9,
        fontstyle='oblique'
    )
    # # g = g + geom_point()
    # # g = g + geom_jitter()
    # # g = g + lims(y=-2500)
    g = g + G.facet_wrap('dataset', scales="free_y")
    # g = g + G.facet_wrap('category + dataset', scales="free_y", ncol=3)
    # g = g + G.facet_grid('. ~ dataset + category', scales="free")
    # g = g + G.facet_grid('. ~ dataset', scales="free")  # row ~ column
    # g = g + G.ylim(0, 650)
    # # g = g + xlim(-1, 3700)
    # # g = g + G.scale_y_log10()
    g = g + G.theme(
        # axis_text_x=element_text(rotation=45, hjust=1),
        # legend_position='top',
        subplots_adjust={'wspace': 0.21},
        figure_size=(15, 6))
    # g = g + G.ggtitle('RW mix 0.1')
    print(g)
    # G.save_as_pdf_pages([g], filename='space-result.pdf')
    # g.save(filename='space-result.eps', height=15, width=5, units='cm', dpi=300, verbose=True)


def spaceDetailData(datasets=None, dbs=None, dataSize='.1'):  # 总空间、空间明细
    if datasets is None:
        datasets = ['energy', 'syn', 'traffic']  #
    if dbs is None:
        dbs = ['TGK', 'PG', 'MA', 'N1', 'N2']
    tmp = []
    for dataset in datasets:
        systems = {}
        for db in dbs:
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        df['dataset'] = dataset
        tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)
    allData['value'] = allData['value']
    return allData


def spaceIncrement():  # 空间增长率分析
    raw = {
        '0.01': spaceDetailData(dataSize='.01'),
        '0.1': spaceDetailData(dataSize='.1'),
        '0.5': spaceDetailData(dataSize='.5'),
        '0.9': spaceDetailData(dataSize='.9'),
        '1': spaceDetailData(dataSize='.all')
    }
    df = dictKey2PdCol(raw, 'size')
    print(df)
    tpData = df.loc[df['category'].isin(['tData', 'tIndex'])]
    print(tpData)
    grp = tpData.groupby(['sys', 'dataset', 'size']).agg('sum')
    toPrint = pd.DataFrame(grp.reset_index())
    print(toPrint)
    toPrint['size'] = toPrint['size'].astype(float)
    # tpData = toPrint.loc[:,['sys', 'dataset', 'value', 'tSize']]
    # tpData
    # toPrint1 = toPrint.pivot(index=['sys', 'dataset'], columns='size', values='value')
    # toPrint2 = toPrint1.sort_values(by=['dataset', 'sys'])
    # toPrint2['inc_rate'] = toPrint2['.1'] / toPrint2['.01']
    # print(toPrint2)
    g = G.ggplot(toPrint, G.aes('size', 'value', color='sys'))
    # g += G.geom_col()
    g += G.geom_line()
    g += G.geom_point()
    # + G.scale_x_datetime(breaks=date_breaks('1 month'))
    # g = g + G.ggtitle('Space cost of Comparable systems (Bytes)')
    g += G.scale_y_continuous(labels=lambda l: ["{:.1f} GB".format(v / 1024/1024/1024) for v in l])
    g += G.ylab('Database Size')
    # g += G.xlab('Systems')

    dodge_text = G.position_dodge(width=.9)
    # g = G.ggplot(spaces, G.aes(x='type', y='exeTime', fill='sys'))
    # g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    # g = g + G.geom_boxplot(width=.3, outlier_size=1, outlier_shape='.', position=dodge_text)
    # g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.sum),
    #                     position=dodge_text, format_string='{:.1f}') #, va='bottom'
    # # g = g + geom_point()
    # # g = g + geom_jitter()
    # # g = g + lims(y=-2500)
    # g = g + G.facet_wrap('dataset', scales="free_y")
    g = g + G.facet_wrap('dataset', scales="free_y", ncol=3)
    # g = g + G.facet_grid('. ~ dataset + category', scales="free")
    # g = g + G.facet_grid('. ~ dataset', scales="free")  # row ~ column
    # g = g + G.ylim(0, 650)
    # # g = g + xlim(-1, 3700)
    # # g = g + G.scale_y_log10()
    g = g + G.theme(
        # axis_text_x=element_text(rotation=45, hjust=1),
        # legend_position='top',
        subplots_adjust={'wspace': 0.21},
        figure_size=(15, 6))
    # g = g + G.ggtitle('RW mix 0.1')
    print(g)

def spaceEnergyTG():
    tmp = []
    dataset = 'energy'
    # db = 'TGK'
    for dataSize in ['.01', '.1', '.5', '.9', '.all']:
        systems = {}
        for db in ['TGK', 'PG', 'MA', 'N1', 'N2']:
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        df['tp_size'] = dataSize
        # df['dataset'] = dataset
        tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)
    allData['value'] = allData['value'] / 1000000
    print(allData)
    allData = allData.loc[allData['category'].isin(['tData', 'tIndex'])]
    totalSize = allData.groupby(['sys', 'tp_size']).agg('sum')
    print(totalSize)
    allData = pd.DataFrame(totalSize.reset_index())
    toPrint1 = allData.pivot(index=['sys'], columns='tp_size', values='value')
    toPrint1['.1/.01'] = toPrint1['.1'] / toPrint1['.01']
    toPrint1['.5/.1'] = toPrint1['.5'] / toPrint1['.1']
    toPrint1['.9/.5'] = toPrint1['.9'] / toPrint1['.5']
    toPrint1['1/.9'] = toPrint1['.all'] / toPrint1['.9']
    # return allData
    print(toPrint1)



def spaceSyn():
    tmp = []
    dataset = 'syn'
    # db = 'TGK'
    for dataSize in ['.01']:
        systems = {}
        for db in ['TGK', 'PG', 'MA', 'N1', 'N2']:
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        df['tp_size'] = dataSize
        # df['dataset'] = dataset
        tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)
    allData['value'] = allData['value'] / 1000000
    print(allData)
    allData = allData.loc[allData['category'].isin(['tData', 'tIndex'])]
    totalSize = allData.groupby(['sys', 'tp_size']).agg('sum')
    print(totalSize)
    allData = pd.DataFrame(totalSize.reset_index())
    toPrint1 = allData.pivot(index=['sys'], columns='tp_size', values='value')
    # toPrint1['.1/.01'] = toPrint1['.1'] / toPrint1['.01']
    # toPrint1['.5/.1'] = toPrint1['.5'] / toPrint1['.1']
    # toPrint1['.9/.5'] = toPrint1['.9'] / toPrint1['.5']
    # toPrint1['1/.9'] = toPrint1['.all'] / toPrint1['.9']
    # return allData
    print(toPrint1)


def spaceCompressOnEnergy():
    tmp = []
    dataset = 'syn'
    # db = 'TGK'
    for dataSize in ['.1']:
        systems = {}
        for db in ['TGK', 'PG']:
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        df['tp_size'] = dataSize
        # df['dataset'] = dataset
        tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)
    allData['value'] = allData['value'] / 1000000
    print(allData)
    allData = allData.loc[allData['category'].isin(['tData', 'tIndex'])]
    totalSize = allData.groupby(['sys', 'tp_size']).agg('sum')
    print(totalSize)
    allData = pd.DataFrame(totalSize.reset_index())
    toPrint1 = allData.pivot(index=['sys'], columns='tp_size', values='value')
    # toPrint1['.1/.01'] = toPrint1['.1'] / toPrint1['.01']
    # toPrint1['.5/.1'] = toPrint1['.5'] / toPrint1['.1']
    # toPrint1['.9/.5'] = toPrint1['.9'] / toPrint1['.5']
    # toPrint1['1/.9'] = toPrint1['.all'] / toPrint1['.9']
    # return allData
    print(toPrint1)


def spaceSynVar():
    tmp = []
    dataSize = '.01'
    # for dataset in ['syna', 'synd', 'syndd']:  #'', 'sync', 'syndf', 'syndff', , , 'synf'
    # for dataset in ['syna', 'syne', 'syneeee']:
    # for dataset in ['syna', 'synb']:
    # for dataset in ['syna', 'sync']:
    # for dataset in ['syna', 'synf', 'synff']:
    for dataset in ['synd', 'syndf', 'syndff']:
        systems = {}
        for db in ['TGK', 'PG', 'N1', 'MA', 'N2']:  #
            try:
                systems[db] = calcSpace(dataset, db, dataSize)
            except DataNotReadyErr as e:
                print(e)
        space4dataset = pd.DataFrame(systems)
        print(space4dataset)
        space4dataset['category'] = space4dataset.index.values
        df = pd.melt(space4dataset, id_vars=['category'], var_name='sys')
        # df['tp_size'] = dataSize
        df['dataset'] = dataset
        tmp.append(df)
    allData = pd.concat(tmp, ignore_index=True)
    allData['value'] = allData['value'] #/ 1024 / 1024 / 1024
    # print(allData)
    allData = allData.loc[allData['category'].isin(['tData', 'tIndex'])]  #
    # print(allData)
    totalSize = allData.groupby(['sys', 'dataset']).agg('sum')
    print(totalSize)
    allData = pd.DataFrame(totalSize.reset_index())
    print(allData)
    # toPrint1 = allData.pivot(index=['sys'], columns='dataset', values='value')
    # print(toPrint1)
    # toPrint2 = toPrint1 / toPrint1.min()
    # print(toPrint2)
    # toPrint1['d'] = toPrint1['synd'] / toPrint1['synd'].min()
    # toPrint1['dd'] = toPrint1['syndd'] / toPrint1['syndd'].min()
    # toPrint1['d/a'] = toPrint1['synd'] / toPrint1['syna']
    # toPrint1['dd/a'] = toPrint1['syndd'] / toPrint1['syna']
    # dataset2size = {'syna':3, 'syne':9, 'syneeee':21}
    # allData['#prop'] = allData['dataset'].apply(lambda ds: dataset2size[ds])
    # dataset2t = {'syna':2000, 'synd': 400, 'syndd': 200}
    # dataset2r = {'syna': 0.5, 'synb': 1}
    # dataset2up = {'syna': 0.05, 'sync': 0.5}
    dataset2e = {'syna': 400000, 'synd': 400000, 'syndf': 4000000, 'syndff': 20000000, 'synf':4000000, 'synff': 40000000}
    allData['param'] = allData['dataset'].apply(lambda ds: dataset2e[ds])
    g = G.ggplot(allData, G.aes('param', 'value', color='sys'))
    # g += G.geom_col()
    g += G.geom_line()
    g += G.geom_point()
    # + G.scale_x_datetime(breaks=date_breaks('1 month'))
    # g = g + G.ggtitle('Space cost of Comparable systems (Bytes)')
    g += G.scale_y_continuous(labels=lambda l: ["%dGB" % (v / 1024/1024/1024) for v in l])
    g += G.ylab('Database Disk Space Size')
    g += G.xlab('Params')

    dodge_text = G.position_dodge(width=.9)
    # g = G.ggplot(spaces, G.aes(x='type', y='exeTime', fill='sys'))
    # g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    # g = g + G.geom_boxplot(width=.3, outlier_size=1, outlier_shape='.', position=dodge_text)
    # g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.sum),
    #                     position=dodge_text, format_string='{:.1f}') #, va='bottom'
    # # g = g + geom_point()
    # # g = g + geom_jitter()
    # # g = g + lims(y=-2500)
    # g = g + G.facet_wrap('dataset', scales="free_y")
    # g = g + G.facet_wrap('dataset', scales="free_y", ncol=3)
    # g = g + G.facet_grid('. ~ dataset + category', scales="free")
    # g = g + G.facet_grid('. ~ dataset', scales="free")  # row ~ column
    # g = g + G.ylim(0, 650)
    # # g = g + xlim(-1, 3700)
    # # g = g + G.scale_y_log10()
    g = g + G.theme(
        # axis_text_x=element_text(rotation=45, hjust=1),
        legend_position='top',
        # subplots_adjust={'wspace': 0.21},
        figure_size=(6, 6))
    # g = g + G.ggtitle('RW mix 0.1')
    print(g)



def entityHistory():
    dataSize = '.01'
    maxConn = 1
    tmp = []
    for dataset in ['energy', 'traffic', 'syn']:
        systems = {}
        for db in ['TGK', 'PG', 'MA', 'N1', 'N2']:
            try:
                systems[db] = fetchLog(lastTestName(dataset, db, dataSize, maxConn))
            except DataNotReadyErr as e:
                print(e)
        if len(systems) > 0:
            df = dictKey2PdCol(systems, 'sys')
            # print(df)
            df['dataset'] = dataset
            tmp.append(df)

    allData = pd.concat(tmp, ignore_index=True)
    data = normalize(allData)
    data = extractParam2Col(data, ['node'])
    # # Setting multiple items using a mask
    # mask = dfd['a'].str.startswith('o')
    # dfd.loc[mask, 'c'] = 42
    print(data)
    data = data.loc[lambda x: x['txSuccess']]
    # data = data.loc[data['type'].isin(['R:E.History', 'W:E.Temp.Edit', 'W:Temp.Append'])]
    # print(data.groupby(['type','sys']).agg('count'))
    # print(data)
    dodge_text = G.position_dodge(width=.9)
    g = G.ggplot(data, G.aes(x='sys', y='exeTime', fill='sys'))
    g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    g = g + G.geom_boxplot(width=.3, alpha=0.7, outlier_size=1, outlier_shape='.', outlier_colour="steelblue",
                           position=dodge_text)
    g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.mean),  # size=7, color='black',
                        position=dodge_text, format_string='{:.1f}', va='bottom', nudge_y=30)
    # g = g + geom_point()
    # g = g + geom_jitter()
    # g = g + lims(y=-2500)
    # g = g + facet_grid('dataSize ~ .', scales = "free")
    g = g + G.facet_grid('node ~ dataset', scales="free")  # row ~ column
    # g = g + G.ylim(0, 250)
    # g = g + xlim(-1, 3700)
    # g = g + G.scale_y_log10()
    g += G.ylab('Request Execution Time (ms)')

    g = g + G.theme(
        axis_text_x=G.element_blank(),  # G.element_text(rotation=30, hjust=0.7, colour="black", size=8), #
        legend_position='bottom',
        # panel_background=G.element_blank(),
        figure_size=(12, 6))
    # g = g + G.ggtitle('Read/Write Mixin on Energy Dataset. (TP size 0.1)')
    print(g)

def timeResults(test, reqCnt=100, queryTpSize='.01', milestoneTpSize='.all'):
    def findValidTest(dataset, db, testName, maxCon):
        tests = lastTestNameByTest(dataset, db, testName, maxCon)
        for test in tests:
            info = TestNameInfo(test[0])
            if info.mtpsize=='all' and info.qtpsize=='T.all':
                print(test)
                return test[0]
        raise DataNotReadyErr('test not found on {} {} {} {}'.format(dataset, db, testName, maxCon))
    tmp = []
    maxCon = 1
    for dataset in ['energy', 'traffic', 'syn']:
        systems = {}
        for db in ['TGK', 'N1', 'N2', 'PG', 'MA']:  #
            try:
                testName = findValidTest(dataset, db, test, maxCon)
                raw = fetchLog(testName)
                if len(raw)==0:
                    print('no data: '+test)
                else:
                    systems[db] = raw
                # print(systems[db])
            except DataNotReadyErr as e:
                print('WARNING: '+e.message)
        if len(systems) > 0:
            df = dictKey2PdCol(systems, 'system')
            # print(df)
            df['dataset'] = dataset
            tmp.append(df)
    data = pd.concat(tmp, ignore_index=True)
    # data = extractParam2Col(data, ['id'])
    data = extractParam2Col(data, ['node', 'id'])
    # print(data.groupby(['dataset', 'node']).agg('count'))
    # print(data[data['txSuccess'].isna()])
    # print(data['id'])
    q = QueryCorrectInfo()
    correct = data.apply(lambda row: q.isCorrect(row['id']), axis=1)
    q.close()
    # print(correct)
    print(data[correct])
    data = data[correct]
    # data = data.loc[lambda x: x['txSuccess']]
    # print(data['txSuccess'].unique())
    data = data.loc[data['system'].isin(['TGK', 'MA', 'PG'])]
    # data = data.loc[data['type'].isin(['R:E.History', 'W:E.Temp.Edit', 'W:Temp.Append'])]
    # print(data.groupby(['type','sys']).agg('count'))
    # print(data)
    dodge_text = G.position_dodge(width=.9)
    g = G.ggplot(data, G.aes(x='system', y='exeTime', fill='system'))
    g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    g = g + G.geom_boxplot(width=.3, alpha=0.7, outlier_size=1, outlier_shape='.', outlier_colour="steelblue",
                           position=dodge_text)
    g = g + G.geom_jitter(alpha=0.7, shape='.', size=0.5, stroke=0.3)
    g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.mean),  # size=7, color='black',
                        position=dodge_text, format_string='{:.1f}', va='bottom', nudge_y=40)
    # g = g + G.geom_point()
    # g = g + lims(y=-2500)
    # g = g + facet_grid('dataSize ~ .', scales = "free")
    g = g + G.facet_grid('node ~ dataset')  # row ~ column , scales="free"
    # g = g + G.facet_wrap('dataset', ncol=3) #   scales="free_y",
    # g = g + G.ylim(0, 5000)
    # g = g + xlim(-1, 3700)
    # g = g + G.scale_y_log10()
    g += G.ylab('Request Execution Time (ms)')

    g = g + G.theme(
        # axis_text_x=G.element_blank(),  # G.element_text(rotation=30, hjust=0.7, colour="black", size=8), #
        # legend_position='bottom',
        # panel_background=G.element_blank(),
        figure_size=(12, 6))
    g = g + G.ggtitle(test)
    print(g)


def buildTime():
    tmp = []
    for mSize in ['.01', '.1', '.5', '.9', '.all']:
        for dataset in ['energy', 'traffic', 'syn']:
            for db in ['TGC', 'TGK', 'N1', 'N2', 'PG', 'MA']:  #
                try:
                    tmpList = milestoneBuildTime(dataset, db, mSize)
                    for item in tmpList:
                        tmp.append(item)
                    # print(systems[db])
                except DataNotReadyErr as e:
                    print('WARNING: '+e.message)
    data = pd.DataFrame(tmp)
    print(data)

    dodge_text = G.position_dodge(width=.9)
    g = G.ggplot(data, G.aes(x='size', y='duration', fill='db'))
    g = g + G.stat_summary(fun_y=np.mean, geom='bar', position=dodge_text, width=.9)
    # g = g + stat_summary(fun_y=np.mean, geom=geom_text(, size=8))
    g = g + G.geom_boxplot(width=.3, alpha=0.7, outlier_size=1, outlier_shape='.', outlier_colour="steelblue",
                           position=dodge_text)
    g = g + G.geom_jitter(alpha=0.7, shape='.', size=0.5, stroke=0.3)
    g = g + G.geom_text(G.aes(label=G.after_stat('y')), stat=G.stat_summary(fun_y=np.mean),  # size=7, color='black',
                        position=dodge_text, format_string='{:3g}', va='bottom', nudge_y=-40)
    # g = g + G.geom_point()
    # g = g + lims(y=-2500)
    # g = g + facet_grid('dataSize ~ .', scales = "free")
    # g = g + G.facet_grid('dataset ~ db', scales="free")  # row ~ column , scales="free"
    g = g + G.facet_grid('dataset ~ size', scales="free")  # row ~ column , scales="free"
    # g = g + G.facet_wrap('dataset', ncol=3) #   scales="free_y",
    # g = g + G.ylim(0, 5000)
    # g = g + xlim(-1, 3700)
    # g = g + G.scale_y_log10()
    g += G.ylab('Milestone Build Time (seconds)')

    g = g + G.theme(
        # axis_text_x=G.element_blank(),  # G.element_text(rotation=30, hjust=0.7, colour="black", size=8), #
        # legend_position='bottom',
        # panel_background=G.element_blank(),
        figure_size=(12, 10))
    # g = g + G.ggtitle(test)
    print(g)


if __name__ == "__main__":
    # spaceSynVar()
    # spaceIncrement()
    spaceAnalysis()
    # spaceEnergyTG()
    # buildTime()
    # timeResults('ehistory')
    # timeResults('snapshot')
    # timeResults('aggmax')
    # timeResults('reachable')
    # timeResults('etpc')
